package com.programmercarl.backtracking;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * https://leetcode.cn/problems/combination-sum-iii/
 *
 * @author yulongtian
 * @create 2024-12-29 10:39
 */
public class Test02 {
    public static void main(String[] args) {

    }


    public List<List<Integer>> combinationSum3(int k, int n) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        Deque<Integer> path = new LinkedList<Integer>();

        backTracking(k, n, 1, res, path, 0);

        return res;
    }

    public void backTracking(int k, int n, int startIndex, List<List<Integer>> res, Deque<Integer> path, int sum) {
        if (path.size() == k) {
            if (sum == n) {
                res.add(new ArrayList<>(path));
            }
            return;
        }

        for (int i = startIndex; i <= 9; i++) {
            path.push(i);
            sum += i;
            backTracking(k, n, i + 1, res, path, sum);
            path.pop();
            sum -= i;
        }
    }


}
